Summary

N Active Users Swap Trades Trades_Per User Trading Volume
ALL 5871 0.7 0.8 1 0.3
KAR:KSM 1606 1.1 0.8 0.7 0.9
KUSD:KSM 709 0.9 1.4 1.3 0.4
KUSD:LKSM 390 0.7 0.3 0.4 0.6
LKSM:KSM 346 0.6 0.5 0.7 0.5
KUSD:KAR 332 0.6 1.1 1.6 0.8
KSM:RMRK 283 0.9 0.8 0.9 0.3
KUSD:RMRK 210 0.4 0.6 1.7 0.2
KSM:BNC 153 1 1.1 0.9 0.7
KAR:RMRK 122 1.1 1.3 1.2 0.2
KAR:LKSM 122 0.5 0.9 1.7 3
KUSD:BNC 112 0.5 0.3 0.4 0.1
LKSM:BNC 44 0.6 0.5 0.9 0.1
KAR:BNC 38 0.7 0.8 1.2 2.3
BNC:RMRK 34 0.3 0.2 0.7 0
LKSM:RMRK 25 0.5 0.4 0.9 0.2

Last updated: 2022-02-09 19:05:51

Date range of data: 2022-02-02T03:26:48.197 to 2022-02-09T23:56:06.375.

Source: SubQuery Network

---
title: "Karura Swap Performance"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
runtime: shiny
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}

knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)


# helper function to color the bars in the charts (green, yellow, & red)
getColor <- function(status) {
  ifelse(status >= 1, "#34FF33", ifelse(status >= .5, "#FFC300", "#FF5733"))
}

# remotes::install_github("ropensci/ghql") # if package is not already installed
library(jsonlite)
library(data.table)
library("ghql")
x <- GraphqlClient$new()

endpoint <- "https://api.polkawallet.io/acala-subql"
window <- 8 # days to read in (latest day plus 7 day average)

# make a client
cli <- GraphqlClient$new(
  url = endpoint,
  headers = list("Accept-Encoding: gzip, deflate, br",
                 "Content-Type: application/json",
                 "Accept: application/json",
                 "Connection: keep-alive",
                 "DNT: 1",
                 "Origin: https://api.polkawallet.io")
)

mindate <- today(tzone = 'UTC') - window

resList <- list()
for (i in 1:1000) {
  offset <- i * 100
  qry <- Query$new()
  qry$query('dexActions', '{
    query {
      dexActions(filter: {timestamp: {greaterThanOrEqualTo: "' %+% mindate %+% '"}}
            			first: 100
      						offset: ' %+% offset %+% ') {
        nodes {
          timestamp
          id
          nodeId
          accountId
          type
          token0Id
          token1Id
          token0Amount
          token1Amount
          volumeUSD
          extrinsicId
          data
        }
      }
    }
  }')
  result <- cli$exec(qry$queries$dexActions) %>%
    fromJSON(flatten=TRUE)
  res <- as.data.table(result$data$query$dexActions$nodes)
  print(i %+% " " %+% nrow(res))
  if (nrow(res) == 0) break
  resList[[i]] <- res
}
res <- rbindlist(resList)

if (substr(max(res$timestamp), 12, 13) < 23) {
  maxdate <- as.Date(max(res$timestamp))-1
} else {
  maxdate <- as.Date(max(res$timestamp))
}

res <- res[timestamp <= maxdate]

res[, data := NULL]
res[, date := as.Date(timestamp)]
setorder(res, timestamp)

# Replace foreign assets
res[token0Id == 'fa://0', token0Id := 'RMRK']
res[token1Id == 'fa://0', token1Id := 'RMRK']

# Create pairs
res[, pair := paste0(res$token0Id, ":", res$token1Id)]
res[, exclude := token0Id == token1Id]

sort(unique(res$pair))

# Normalize pairs
res[pair == "KSM:KAR",  pair := "KAR:KSM"]
res[pair == "KSM:LKSM", pair := "LKSM:KSM"]
res[pair == "KSM:KUSD", pair := "KUSD:KSM"]

res[pair == "LKSM:KAR", pair := "KAR:LKSM"]
res[pair == "LKSM:KUSD",pair := "KUSD:LKSM"]
res[pair == "LKSM:BNC", pair := "BNC:LKSM"]

res[pair == "BNC:KAR",  pair := "KAR:BNC"]
res[pair == "BNC:LKSM", pair := "LKSM:BNC"]
res[pair == "BNC:KUSD", pair := "KUSD:BNC"]

res[pair == "KAR:KUSD", pair := "KUSD:KAR"]
res[pair == "BNC:KSM",  pair := "KSM:BNC"]
res[pair == "BNC:KSM",  pair := "KSM:BNC"]

res[pair == "RMRK:BNC", pair := "BNC:RMRK"]
res[pair == "RMRK:KAR", pair := "KAR:RMRK"]
res[pair == "RMRK:KSM", pair := "KSM:RMRK"]
res[pair == "RMRK:KUSD", pair := "KUSD:RMRK"]
res[pair == "RMRK:LKSM", pair := "LKSM:RMRK"]

pairs <- rbind(res[exclude == FALSE & type == 'swap', .N, by = pair], data.table(pair="ALL", N = res[, .N]))[order(N, decreasing = TRUE)] %>% 
  setnames(c("Pair", "Observations"))

# Calculate measures for each pair
user_status   <- list()
trades_status <- list()
tpu_status    <- list()
volume_status <- list()

users_list  <- list()
trades_list <- list()
per_list    <- list()
volume_list <- list()

for (p in pairs$Pair) {
  
  try(rm(u_list, t_list, p_list, v_list), silent = TRUE)
  
  outname <- "~/R_HOME/websites/web_acala/content/swap_" %+% p %+% ".html"
  unlink(outname)
  # Create report for each pair
  rmarkdown::render("~/R_HOME/karura-reports/Swap_template.Rmd",
                  output_file = outname,
                  params = list(pair = p))
  
  # Store the data for the table
  user_status[p]   <- activeUsersStatus
  trades_status[p] <- tradesStatus
  tpu_status[p]    <- avgTradeStatus
  volume_status[p] <- tradeVolumeStatus
  
  users_list[[p]]  <- u_list
  trades_list[[p]] <- t_list
  per_list[[p]]    <- p_list
  volume_list[[p]] <- v_list

}

  d <- list()
  for (x in pairs$Pair) {
    d[x] <- paste0('', 
                     x, 
                     '', collapse = "")
  }

  inline_plot <- data.frame(N = pairs$Observations, 
                            Active = unlist(user_status),
                            Users = "",
                            Swap = unlist(trades_status),
                            Trades = "",
                            Trades_Per = unlist(tpu_status),
                            User = "",
                            Trading = unlist(volume_status),
                            Volume = "")
  row.names(inline_plot) <- unlist(d)

  inline_plot$Active <- cell_spec(inline_plot$Active, color = ifelse(inline_plot$Active < 1, "red", "green"))

  inline_plot$Swap <- cell_spec(inline_plot$Swap, color = ifelse(inline_plot$Swap < 1, "red", "green"))
  
  inline_plot$Trades_Per <- cell_spec(inline_plot$Trades_Per, color = ifelse(inline_plot$Trades_Per < 1, "red", "green"))

  inline_plot$Trading <- cell_spec(inline_plot$Trading, color = ifelse(inline_plot$Trading < 1, "red", "green"))

  p <- inline_plot %>%
    kbl(booktabs = TRUE, escape = FALSE) %>%
    kable_paper(full_width = FALSE) %>%
    column_spec(4, image = spec_plot(users_list, same_lim = FALSE)) %>%
    column_spec(6, image = spec_plot(trades_list, same_lim = FALSE)) %>%
    column_spec(8, image = spec_plot(per_list, same_lim = FALSE)) %>%
    column_spec(10, image = spec_plot(volume_list, same_lim = FALSE))

```

### Summary

```{r plot, result='asis', out.height = 12}
p
```

Last updated: `r Sys.time()`

Date range of data: `r min(res$timestamp)` to `r max(res$timestamp)`.

Source: [SubQuery Network](https://explorer.subquery.network/)